import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_widgets_pc/utils/toast_util.dart';

/// 文件选择工具
///
/// 包括图片或文件选择
/// 其中文件选择仅支持Android端，iOS只支持选择图片
class FilePickerNativeUtil {
  static const _methodChannelName = 'flutter.keqiang.com/filePicker';

  static const _platformMethod = MethodChannel(_methodChannelName);

  /// 拍照
  static Future<KqFile?> takePhoto() async {
    try {
      var result = await _platformMethod.invokeMethod('takePhoto');
      if (result['code'] == 1) {
        dynamic e = result['data'];
        return KqFile(
            fileName: e['fileName'],
            filePath: e['filePath'],
            fileLength: e['fileLength'],
            mime: e['mime'],
            suffix: e['suffix']);
      } else if (result['code'] == 0) {
        var msg = result['msg'];
        KqToast.showNormal(msg);
      }
      return null;
    } on PlatformException catch (e) {
      debugPrint(e.message);
      return null;
    }
  }

  /// 选择图片
  static Future<List<KqFile>> pickImages({int maxCount = 1}) async {
    try {
      var result = await _platformMethod
          .invokeMethod('pickImages', {'maxCount': maxCount});
      if (result['code'] == 1) {
        List<dynamic> data = result['data'];
        return data
            .map((e) => KqFile(
                fileName: e['fileName'],
                filePath: e['filePath'],
                fileLength: e['fileLength'],
                mime: e['mime'],
                suffix: e['suffix']))
            .toList();
      } else if (result['code'] == 0) {
        var msg = result['msg'];
        KqToast.showNormal(msg);
      }
      return [];
    } on PlatformException catch (e) {
      debugPrint(e.message);
      return [];
    }
  }

  /// 选择指定类型文件
  static Future<List<KqFile>> pickFiles(
      {List<String>? mimes, int maxCount = 1}) async {
    try {
      var result = await _platformMethod.invokeMethod(
          'pickFiles', {'maxCount': maxCount, 'mimes': mimes ?? []});
      if (result['code'] == 1) {
        List<dynamic> data = result['data'];
        return data
            .map((e) => KqFile(
                fileName: e['fileName'],
                filePath: e['filePath'],
                fileLength: e['fileLength'],
                mime: e['mime'],
                suffix: e['suffix']))
            .toList();
      } else if (result['code'] == 0) {
        var msg = result['msg'];
        KqToast.showNormal(msg);
      }
      return [];
    } on PlatformException catch (e) {
      debugPrint(e.message);
      return [];
    }
  }
}

/// 文件选择结果
class KqFile {
  const KqFile(
      {this.fileName, this.filePath, this.fileLength, this.suffix, this.mime});

  /// 文件名
  final String? fileName;

  /// 文件路径
  final String? filePath;

  /// 文件大小
  final int? fileLength;

  /// 文件后缀
  final String? suffix;

  /// MIME
  final String? mime;

  @override
  String toString() {
    return '''{"fileName"": "$fileName","filePath": "$filePath","suffix": "$suffix","fileLength": $fileLength,"mime": "$mime"}''';
  }
}

/// 常用文件类型的MIME
class MIME {
  /// .txt
  static String get txt => "text/plain";

  /// .html
  static String get html => "text/html";

  /// .csv
  static String get csv => "text/csv";

  /// .json
  static String get json => "application/json";

  /// .jpg
  static String get jpeg => "image/jpeg";

  /// 所有图片
  static String get image => "image/*";

  /// .png
  static String get png => "image/png";

  /// .gif
  static String get gif => "image/gif";

  /// .webp
  static String get webp => "image/webp";

  /// .pdf
  static String get pdf => "application/pdf";

  /// .zip
  static String get zip => "application/zip";

  /// .rar
  static String get rar => "application/x-rar-compressed";

  /// .jar
  static String get jar => "application/java-archive";

  /// .xls
  static String get xls => "application/vnd.ms-excel";

  /// .xlsx
  static String get xlsx =>
      "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

  /// .doc
  static String get doc => "application/msword";

  /// .docx
  static String get docx =>
      "application/vnd.openxmlformats-officedocument.wordprocessingml.document";

  /// .mp3
  static String get mp3 => "audio/mpeg";

  /// .wav
  static String get wav => "audio/wav";

  /// .apk
  static String get apk => "application/vnd.android.package-archive";

  /// 未知
  static String get unknown => "application/octet-stream";
}
